package com.product.boot;

import com.product.config.SenderConfig;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author
 * @name：
 * @data:
 * @className:
 **/
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class sender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void testDelayMsg() {
        rabbitTemplate.convertAndSend("delay.topic", "day", "雄霸天下", message -> {
            MessageProperties messageProperties = message.getMessageProperties();
            messageProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
            //设置消息5秒后才会被消费
            messageProperties.setHeader("x-delay", 1000);
            return message;
        });
        log.info("消息发送成功");
    }

    @Test
    public void sendMessage() {
        String message = "欢迎光临";
        rabbitTemplate.convertAndSend("direct.exchange","pig",message);
        System.out.println("消息发送成功！");
    }

}
